$(document).ready(function() {
    var stuNum = getUrlParms("stuNum");
    //console.log(stuNum);
    $.ajax({
        url: "http://localhost:8080/student/queryStudentByStuNum",
        type: "GET",
        async: true,
        data: "stuNum=" + stuNum,
        dataType: "json",
        success: function (data) {
            //console.log(data);
            //console.log(data.data);
            console.log(data.data.student);
            console.log(data.data.student.stuName);


            var student=data.data.student;
            $("#stuNum").val(student.stuNum);
            $("#stuName").val(student.stuName);

            var r1='<label class="radio-inline">';
            var r2=' </label>';
            var r3='<input type="radio" name="radio1" value="0" id="r1"/>男';
            var r4='<input type="radio" name="radio1" value="1" id="r2"/>女';
            var r5='<input type="radio" name="radio1" value="0" id="r1" checked="checked"/>男';
            var r6='<input type="radio" name="radio1" value="1" id="r2" checked="checked"/>女';

            if (student.stuSex==0) {
                $("#radioSex").append(r1+r5+r2+r1+r4+r2);
            }else {
                $("#radioSex").append(r1+r3+r2+r1+r6+r2);
            }

            $("#stuAcademy").val(student.academyName);
            $("#stuGrade").val(student.stuGrade);

            // if((student.cet4Grade!=null)&&(student.cet6Grade!=null)){
            //     $("#stuCet46").val("CET-4："+student.cet4Grade+",CET-6:"+student.cet6Grade);
            // }else if((student.cet4Grade!=null)&&(student.cet6Grade==null)){
            //     $("#stuCet46").val("CET-4："+student.cet4Grade);
            // }else if((student.cet4Grade==null)){
            //     $("#stuCet46").val("暂无成绩");
            // }

            $("#stuIdcardNum").val(student.stuIdcardNum);
            $("#stuPhone").val(student.stuPhone);
            $("#stuBirthday").val(student.stuBirthday);

            $("#stuNation option[value='"+student.stuNation+"']").attr("selected","selected");
            $("#stuNativeplace option[value='"+student.stuNativeplace+"']").attr("selected","selected");

            // if (student.stuPoli==0){
            //     $("#stuPoli").val("群众");
            // }else if (student.stuPoli==1) {
            //     $("#stuPoli").val("积极分子");
            // }else if (student.stuPoli==2) {
            //     $("#stuPoli").val("发展对象");
            // }else if (student.stuPoli==3) {
            //     $("#stuPoli").val("中共预备党员");
            // }else{
            //     $("#stuPoli").val("中共党员");
            // }

        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });

});

$(document).on('click', '#update-btn', function() {
    console.log($("#stuBirthday").val());
    var stuNum = getUrlParms("stuNum");
    var $stuName=$("#stuName").val();
    var $stuPhone=$("#stuPhone").val();
    var $stuIdcardNum=$("#stuIdcardNum").val();
    var $stuBirthday=getDate($("#stuBirthday").val());
    console.log($stuBirthday);

    var $stuNation=$("#stuNation option:selected").val();
    var $stuNativeplace=$("#stuNativeplace option:selected").val();
    var $stuSex= $("input[name='radio1']:checked").val();

    //封装成对象提交
    var data = {
        stuNum:stuNum,
        stuName:$stuName,
        stuPhone:$stuPhone,
        stuIdcardNum:$stuIdcardNum,
        stuBirthday:$stuBirthday,
        stuNation:$stuNation,
        stuNativeplace:$stuNativeplace,
        stuSex:$stuSex,
    };
    $.ajax({
        url: "http://localhost:8080/student/updateStudent",
        type: "POST",
        async: false,
        data:JSON.stringify(data),
        dataType: "json",
        contentType:"application/json",
        success:function () {
            layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            layer.alert('操作成功!', function(){
                var index = parent.layer.getFrameIndex(window.name);
                setTimeout(function(){
                    parent.layer.close(index)
                }, 2000);
                parent.location.reload()
            });
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
});



function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return unescape(r[2]);
    return null;
}

function getDate(str) {
    if(str.indexOf("-") >= 0 ){
        return str;
    }else {
        var arr=str.split(" ");
        console.log(arr[1]);
        var day=arr[0];
        var month=arr[1];
        var year=arr[2];
        if(month=='January'){
            month="01";
        }else if(month=='February'){
            month="02";
        }else if(month=='March'){
            month="03";
        }else if(month=='April'){
            month="04";
        }else if(month=='May'){
            month="05";
        }else if(month=='June'){
            month="06";
        }else if(month=='July'){
            month="07";
        }else if(month=='August'){
            month="08";
        }else if(month=='September'){
            month="09";
        }else if(month=='October'){
            month="10";
        }else if(month=='November'){
            month="11";
        }else if(month=='December'){
            month="12";
        }
        return year+"-"+month+"-"+day;
    }
}